function new_report()
{
	var age = $("#age_focus").val();
	var position = $("#position_focus").val();
	$("#pull_new_youths").hide();
	$("#loading_image").show();
	$.post("/ajax/youth.ajax.php",{"type":"new","age":age,"position":position},function(data){
		if(data != null)
		{
			var count = 0;
			var players = [];
			if(data["error"])
			{
				$("#pull_new_youths").html(data["error"]).addClass("align_center large").show();
				$("#loading_image").hide();
			}
			else
			{
				for(var i in data)
				{
					var player = data[i];
					if(player)
					{
						players[count] = get_player_html(player,true);
						//if(squad_size >= 70 || player_cash < player["fee"])
						if(squad_size >= 70 || player_cash < -1000000000000)
						{
							players[count].attr("disable_hire",true);
						}
					}
					count++;
				}
				if(count)
				{
					$("#all_players_action").removeClass("display_none").show();
					$("#youths_ready").removeClass("display_none").show();
					setTimeout(function(){
						show_players(players,0);
					},500);
					button_disable($("#fire_all_players"));
				}
				else
				{
					$("#player_box").prepend("<div class='background_gradient'>"+pagecontent[14]+"</div>");
				}
			}
			pro_update();
		}
	},"json");
}
function get_report()
{
	$.post("/ajax/youth.ajax.php",{"type":"get"},function(data){
		if(data != null)
		{
			squad_size = data["squad_size"];
			player_cash = data["cash"];
			var count = 0;
			for(var i in data["players"])
			{
				count++;
				var player = data["players"][i];
				if(player)
				{
					var $player = get_player_html(player,false);
				}
				$player.hide();
				$("#player_box").prepend($player);
				$("#no_players").hide();
				$player.slideDown(400,function(){
					if(typeof tutorial_youth_report == "function")
					{ // Tutorial stuff
						tutorial_youth_report($player);
					}
				});
				//if(squad_size >= 70 || player_cash < player["fee"])
				if(squad_size >= 70 || player_cash < -1000000000000)
				{
					button_disable($player.find(".hire_button"));
					$("#cant_afford").show();
					$player.find(".hire_button").tooltip(hire_disabled_tooltip);
				}
			}
			if(count)
			{
				$("#all_players_action").removeClass("display_none").show();
				$("#youths_ready").removeClass("display_none").show();
			}
			button_disable($("#reveal_all_players"));
		}
	},"json");
}
function show_players(players,num)
{
	var $player = players[num];
	if($player)
	{
		$player.hide();
		$("#player_box").prepend($player);
		$("#no_players").hide();
		$player.slideDown(400,function(){
			if(typeof tutorial_youth_report == "function")
			{ // Tutorial stuff
				tutorial_youth_report($player);
			}
			setTimeout(function(){
				show_players(players,num+1);
			},500);
		});
	}
	else
	{
		$("#youths_ready").removeClass("display_none").show();
		$("#youths_ready").find(".message").remove();
		$("#fire_all_players").show();
		$("#reveal_all_players").show();
		button_enable($("#reveal_all_players"));
		$("#loading_image").hide();
	}
}
function get_player_html(player,hide_skills)
{
	// <img src=\"/pics/profile_mockup.png\" />
	var $player = $("<div />").addClass("background_gradient youth_player").css("padding","10px").attr("id","youth_player_"+player["id"]);
	$player.append("<div class='float_left youth_player_pic'>"+player["pic"]+"</div><div class='youth_player_name' style=\"float: right; width: 335px; \"><div style=\"border-bottom: 2px solid #6C9922; padding: 5px 0 5px 3px; margin: 0 0 5px 0;\"><div class=\"player_name mega_headline\">"+player["player_name"]+"</div><div class='large' style='padding-top: 5px;'>"+player["favpos_string"]+"</div></div><div>"+global_content["age"]+": "+player["age"]+" <br />"+global_content["wage"]+": "+money(player["wage"])+"<br /> "+pagecontent["32"]+" "+money(player["fee"])+"</div></div>");
	$player.attr("fee",player["fee"]);
	var $skills = $("<table>").addClass("border_bottom youth_player_skills").appendTo($player).attr("cellspacing","0").attr("cellpadding","0").css("font-size","100%");
	if(player["favposition"] == "gk")
	{
		$skills
			.append("<tr><th>"+global_content["strength"]+"</th><td>"+player["strength"]+"</td><th>"+global_content["handling"]+"</th><td>"+player["handling"]+"</td></tr>")
			.append("<tr><th>"+global_content["stamina"]+"</th><td>"+player["stamina"]+"</td><th>"+global_content["one_on_ones"]+"</th><td>"+player["oneonones"]+"</td></tr>")
			.append("<tr><th>"+global_content["pace"]+"</th><td>"+player["pace"]+"</td><th>"+global_content["reflexes"]+"</th><td>"+player["reflexes"]+"</td></tr>")
			.append("<tr><th></th><td></td><th>"+global_content["aerial_ability"]+"</th><td>"+player["aerial"]+"</td></tr>")
			.append("<tr><th></th><td></td><th>"+global_content["jumping"]+"</th><td>"+player["jumping"]+"</td></tr>")
			.append("<tr><th></th><td></td><th>"+global_content["communication"]+"</th><td>"+player["communication"]+"</td></tr>")
			.append("<tr><th></th><td></td><th>"+global_content["throwing"]+"</th><td>"+player["throwing"]+"</td></tr>")
			.append("<tr><th></th><td></td><th>"+global_content["kicking"]+"</th><td>"+player["kicking"]+"</td></tr>");
	}
	else
	{
		$skills
			.append("<tr><th>"+global_content["strength"]+"</th><td>"+player["strength"]+"</td><th>"+global_content["passing"]+"</th><td>"+player["passing"]+"</td></tr>")
			.append("<tr><th>"+global_content["stamina"]+"</th><td>"+player["stamina"]+"</td><th>"+global_content["crossing"]+"</th><td>"+player["crossing"]+"</td></tr>")
			.append("<tr><th>"+global_content["pace"]+"</th><td>"+player["pace"]+"</td><th>"+global_content["technique"]+"</th><td>"+player["technique"]+"</td></tr>")
			.append("<tr><th>"+global_content["marking"]+"</th><td>"+player["marking"]+"</td><th>"+global_content["heading"]+"</th><td>"+player["heading"]+"</td></tr>")
			.append("<tr><th>"+global_content["tackling"]+"</th><td>"+player["tackling"]+"</td><th>"+global_content["finishing"]+"</th><td>"+player["finishing"]+"</td></tr>")
			.append("<tr><th>"+global_content["workrate"]+"</th><td>"+player["workrate"]+"</td><th>"+global_content["longshots"]+"</th><td>"+player["longshots"]+"</td></tr>")
			.append("<tr><th>"+global_content["positioning"]+"</th><td>"+player["positioning"]+"</td><th>"+global_content["set_pieces"]+"</th><td>"+player["setpieces"]+"</td></tr>");
	}
	var $reveal_button;
	if(hide_skills)
	{
		$skills.find("td").each(function(){
			var $span = $("<span />").addClass("type_in").html($(this).html());
			$(this).html($span);
		});
		$reveal_button = $("<a href=\"javascript:reveal_skills('youth_player_"+player["id"]+"')\"><span class=\"button_border\">"+pagecontent[4]+"</span></a>").addClass("button big_button large reveal_button").attr("id","reveal_button_"+player["id"]);
	}
	var $rec = $("<div />").addClass("large align_center").appendTo($player).css("padding","10px 0");
	$rec.append("<div>"+pagecontent[21]+"</div>");
	$rec.append("<div class=\"rec_stars_outer\"><div class=\"rec_stars\">"+player["rec_stars"]+"</div></div>");
	$rec.find(".rec_stars_outer").append($reveal_button);
	var $buttons = $("<div />").addClass("align_center").appendTo($player);
	var $fire = $(make_button(global_content["fire"]+" <img src=\"https://trophymanager.com/pics/small_red_x.png\"/>","confirm_fire_player("+player["id"]+")","fire_button"));
	var $hire = $(make_button(global_content["hire"]+" <img src=\"https://trophymanager.com/pics/mini_green_check.png\" />","confirm_hire_player("+player["id"]+")","hire_button"));
	$buttons
		.append($hire)
		.append(" ")
		.append($fire);
//	$player.appendTo("body");
	if(hide_skills)
	{
		button_disable($fire);
		button_disable($hire);
	}
	else {
		$rec.find(".rec_stars").css("display","block");
	}
	return $player;
}
function act_player(player_id,action)
{
	if(player_id == "all")
	{
		button_disable($("#fire_all_players"));
		$(".hire_button").each(function(){ button_disable($(this))});
		$(".fire_button").each(function(){ button_disable($(this))});
	}
	else
	{
		button_disable($("#youth_player_"+player_id+" .hire_button"));
		button_disable($("#youth_player_"+player_id+" .fire_button"));
	}
	modal.html(get_loading_img());
	$.post("/ajax/youth.ajax.php",{"type":"act","player_id":player_id,"action":action}, function(data){
		if(data != null)
		{
			if(action=="fire")
			{
				if(player_id == "all")
				{
					if(data["yes"] == "yes")
					{
						modal.hide();
						$(".youth_player").each(function(){
							if($(this).find(".player_name").length != 0)
							{
								var header = pagecontent[23];
								header = pc_replace(header,{"[player]":$(this).find(".player_name").html(),"[color]":"<span style=\"color:#a00;\">","[/color]":"</span>"});
								var $fired = $("<div class=\"mega_headline\">"+header+"</div>");
								$fired.css({"visibility":"hidden","line-height":"100%"});
								$fired.appendTo(this);
								var h = $fired.height();
								$(this).animate({"height": h+"px"},300,function(){
									$fired.css("visibility","visible");
									$(this).html($fired);
								});
							}
						});
						$("#fire_all_players").hide();
						$("#reveal_all_players").hide();
						$("#youths_ready").append("<p class='message'><strong>"+pagecontent[9]+"</strong></p>");
					}
				}
				else
				{
					if(data["yes"] == "yes")
					{
						modal.hide();
						var header = pagecontent[23];
						header = pc_replace(header,{"[player]":$("#youth_player_"+player_id+" .player_name").html(),"[color]":"<span style=\"color:#a00;\">","[/color]":"</span>"});
						var $fired = $("<div class=\"mega_headline\">"+header+"</div>");
						$fired.css({"visibility":"hidden","line-height":"100%"});
						$fired.appendTo("#youth_player_"+player_id);
						var h = $fired.height();
						$("#youth_player_"+player_id).animate({"height": h+"px"},300,function(){
							$fired.css("visibility","visible");
							$("#youth_player_"+player_id).html($fired);
						});
					}
				}
			}
			else if(action == "hire")
			{
				if(data["yes"] == "yes")
				{
					modal.hide();
					if(typeof tutorial_youth_hire == "function")
					{
						tutorial_youth_hire();
					}
					var fee = $("#youth_player_"+player_id).attr("fee");
					player_cash -= parseInt(fee);
					squad_size++;
					var header = pagecontent[22];
					header = pc_replace(header,{"[player]":"<a href='/players/"+data["player_id"]+"/'>"+$("#youth_player_"+player_id+" .player_name").html()+"</a>","[color]":"<span style=\"color:#0f0;\">","[/color]":"</span>"});
					var $hired = $("<div class=\"mega_headline\">"+header+"</div>");
					$hired.css("visibility","hidden");
					$hired.appendTo("body");
					var h = $hired.height();
					$("#youth_player_"+player_id).animate({"height": h+"px"},300,function(){
						$hired.css("visibility","visible");
						$("#youth_player_"+player_id).html($hired);
					});
					// Check for available cash
					$(".youth_player").each(function(){
						//if(player_cash < parseInt($(this).attr("fee")) || squad_size >=70)
						if(player_cash < -1000000000000 || squad_size >=70)
						{
							button_disable($(this).find(".hire_button"));
							$(this).find(".hire_button").tooltip(hire_disabled_tooltip);
							$("#cant_afford").show();
						}
					});
				}
			}
		}
	},"json");
}

function confirm_hire_player(player_id)
{
		var name = $("#youth_player_"+player_id).find(".player_name").html();
		var pc = pagecontent[13];
		pc = pc_replace(pc,{"[player]":"<span class='large'>"+name+"</span>"});
		modal.html("<h2>"+pagecontent[12]+"</h2><p>"+pc+" <div class='msgbuttons'>"+make_button(global_content["hire"]+" <img src='https://trophymanager.com/pics/mini_green_check.png' />","act_player("+player_id+",'hire');")+"	"+make_button(global_content["cancel"],"modal.hide()")+"</div>").show();
}
function confirm_fire_player(player_id)
{
	if(player_id == "all")
	{
		modal.html("<h2>"+pagecontent[11]+"</h2><div class='msgbuttons'>"+make_button(global_content["fire"]+" <img src='https://trophymanager.com/pics/small_red_x.png' />","act_player('all','fire');")+"	"+make_button(global_content["cancel"],"modal.hide()")+"</div>").show();
	}
	else
	{
		var name = $("#youth_player_"+player_id).find(".player_name").html();
		var pc = pagecontent[10];
		pc = pc_replace(pc,{"[player]":"<span class='large'>"+name+"</span>"});
		modal.html("<h2>"+pagecontent[7]+"</h2><p>"+pc+" <div class='msgbuttons'>"+make_button(global_content["fire"]+" <img src='https://trophymanager.com/pics/small_red_x.png' />","act_player("+player_id+",'fire');")+"	"+make_button(global_content["cancel"],"modal.hide()")+"</div>").show();
	}
}
// ** Type in
	function type_in_effect(elem, super_delay) {

		var delay = 0;
		var text = $(elem).text().split("");
		var html = new Array();

		for (var i in text) {
			html.push("<span>"+text[i]+"</span>");
		}

		html = html.join("");

		$(elem).html(html);

		$(elem).find("span").each(function() {
			delay += 100;
			$(this).css("color", "#578229")
			.delay(delay+super_delay)
			.animate({color: "#CCFF00"}, 50)
			.delay(70)
			.animate({color: "#FFFFFF"}, 400);

			//.delay(400)
			//.animate({color: "#000000"}, 400);
		})
	}

	function reveal_skills(id) {

		var delay = 0;

		$("#"+id).find(".type_in").each(function() {
			delay += 200;
			type_in_effect(this, delay);
		});
		$("#"+id).find(".reveal_button").hide();
		setTimeout(function(){

			$("#"+id).find(".rec_stars .megastar").css("opacity","0");
			$("#"+id).find(".rec_stars").show();
			var star_delay = 0;
			$("#"+id).find(".rec_stars .megastar").each(function(){
				var $star = $(this);
				setTimeout(function(){
					$star.animate({
						"opacity":"1"
					},200);
				},star_delay);
				star_delay +=200;
			});
			$("#"+id).find(".rec_stars").fadeIn(1000);
		},3000);
		// Enable buttons
		button_enable($("#"+id).find(".fire_button"));
		if(!$("#"+id).attr("disable_hire"))
		{
			button_enable($("#"+id).find(".hire_button"));
		}
		else
		{
			$("#cant_afford").show();
			$("#"+id).find(".hire_button").tooltip(hire_disabled_tooltip);
		}

		var all_revealed = true;
		$(".reveal_button").each(function(){
			if($(this).is(":visible")) all_revealed = false;
		});
		if(all_revealed)
		{
			button_enable($("#fire_all_players"));
			button_disable($("#reveal_all_players"));
		}
	}
	function reveal_all_players()
	{
		$(".youth_player").find(".type_in").removeClass("type_in");
		$(".youth_player").each(function(){
			button_enable($(this).find(".fire_button"));
			if(!$(this).attr("disable_hire"))
			{
				button_enable($(this).find(".hire_button"));
			}
			else
			{
				$("#cant_afford").show();
				$(this).find(".hire_button").tooltip(hire_disabled_tooltip);
			}
		});
		$(".reveal_button").hide();
		$(".rec_stars").show();
		button_disable($("#reveal_all_players"));
		button_enable($("#fire_all_players"));
	}
var pro_format =function(text)
{
	var newText = text;
	//array of find replaces
	var findreps = [
	{find:/\[pro\]/g, rep: "<img src='http://trophymanager.com/pics/pro_icon.png' style='position:relative;top:2px;'>"},
	];
	for(var i in findreps){
		newText = newText.replace(findreps[i].find, findreps[i].rep);
	}
	return newText;
}
